package com.zr.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.zr.dao.FuncDAO;
import com.zr.datamodel.FuncDM;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Repository("funcDao")
public class FuncDAOImpl extends BaseDaOImpl<FuncDM> implements FuncDAO {

	@Override
	public JSONArray getFuncById(int parentId) {
		JSONArray array=new JSONArray();
		List<FuncDM> funcs = get(FuncDM.class, parentId).getChildrenFuncs();
		for (FuncDM func : funcs) {
			JSONObject object = new JSONObject();
			object.put("id", func.getId());
			object.put("text", func.getName());
			JSONObject object1 = new JSONObject();
			object1.put("url", func.getUrl());
			object.put("attributes", object1);
			array.add(object);
		}
		return array;
	}

	@Override
	public JSONArray getAllFuncById(int parentId) {
		JSONArray array=this.getFuncById(parentId);
		for (int i = 0; i < array.size(); i++) {
			JSONObject jsObj=array.getJSONObject(i);
			if ("open".equals(jsObj.get("state"))) {
				continue;
			}else {
				jsObj.put("children", getAllFuncById(Integer.parseInt(jsObj.getString("id"))));
			}
		}
		return array;
	}

}
